package springBoot_dzfpxt.custom;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import springBoot_dzfpxt.entity.EimRole;
import springBoot_dzfpxt.entity.EimUser;
import springBoot_dzfpxt.rpository.BoosRepository;
import springBoot_dzfpxt.rpository.EimUserrepository;
import springBoot_dzfpxt.rpository.SuperBoosRepository;

@Component
public class UserUtils {
	@Autowired
	private EimUserrepository userDAO;
	
	public EimUser getUser() {
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String username=userDetails.getUsername();
		EimUser user = userDAO.findByUsername(username);
		return user;
	}
	
	
	public boolean hasRole(String role) {
		List<EimRole> roles=getUser().getRoles();
		List<String> roleNames=new ArrayList<>();
		for (EimRole sysRole : roles) {
			roleNames.add(sysRole.getName());
		}
		return roleNames.contains(role);
	}
}
